package com.tanhua.mangement.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.ManageLogsApi;
import com.tanhua.dubbo.server.enums.LogType;
import com.tanhua.dubbo.server.pojo.Managelogs;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.mangement.vo.ManageLogsVo;
import com.tanhua.mangement.vo.PageLogResult;
import com.tanhua.mangement.vo.PageResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ManageLogsService {

    @Reference(version = "1.0.0")
    private ManageLogsApi manageLogsApi;


    public PageLogResult queryLoginLogList(Long uid, Integer page, Integer pagesize, String sortProp, String sortOrder, LogType login) {

        //翻页
        PageLogResult result = new PageLogResult();
        result.setPage(page);
        result.setPagesize(pagesize);

        PageInfo<Managelogs> info = manageLogsApi.queryLoginLogList(page, pagesize, sortProp, sortOrder, uid, login);

        result.setCounts(info.getTotal());

        List<Managelogs> records = info.getRecords();
        //没有信息直接返回
        if (CollUtil.isEmpty(records)) {
            return result;
        }
        List<ManageLogsVo> voList = new ArrayList<>();
        //填充数据
        for (Managelogs record : records) {
            ManageLogsVo logVo = new ManageLogsVo();
            logVo.setId(Convert.toInt(record.getUserId()));
            logVo.setLogTime(record.getLogTime());
            logVo.setPlace(record.getPlace());
            logVo.setEquipment(record.getEquipment());

            voList.add(logVo);
        }
        result.setItems(voList);
        return result;
    }
}
